#!/bin/bash


BPS=2000000000

CID=$(nerdctl -n k8s.io ps | grep nm | grep -v '/pause' | awk '{print $1}')

for c in ${CID}
do

	pid=$(nerdctl -n k8s.io top ${c} | grep 'java' | awk '{print $2}' | head -n 1)

	if [ -z "${pid}" ]; then
	  continue
	fi

	pod=$(nerdctl -n k8s.io inspect "${c}" | grep 'pod.uid' | awk -F ':' '{print $2}' | sed 's/[" ,]//g')

	dev=$(mount | grep "${pod}.*export" | awk '{print $1}')
	DEV=$(lsblk -o MAJ:MIN ${dev} | tail -n 1)
	CG=$(cat /proc/${pid}/cgroup | awk -F':' '/memory:/ {print $3}')

	blkcg="/sys/fs/cgroup/blkio${CG}"
	memcg="/sys/fs/cgroup/memory${CG}"

	if [[ ! "${memcg}" =~ "yarn" ]]; then
		memcg="${memcg}/yarn"
	fi

	blkcg=${blkcg%/yarn}

	blkino=$(ls -id ${blkcg} | awk '{print $1}')
	echo ${blkino} > ${memcg}/memory.wb_blkio_ino

	echo "${DEV} ${BPS}" > ${blkcg}/blkio.throttle.write_bps_device

done

